<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once 'sitecontroller.php';

class Relatorio extends SiteController {
    
    function __construct(){
        parent::__construct();
        parent::auth();
        
        if ($this->data['user']->getNivel() < 2){
            redirect('/');
        }
        
        $this->data['title'] = 'Relat&oacute;rio';
    }
    
    public function servicos(){
        
        $this->data['dados'] = array();
        $query = $this->db->query("SELECT c.servico as id, s.titulo, s.dificuldade, COUNT(c.id) as num "
                . "FROM chamado c INNER JOIN servico s ON (c.servico = s.id) GROUP BY c.servico");
        if ($query->num_rows() > 0){
            $this->data['dados'] = $query->result();
        }
        
        $this->loadView('relatorio/servicos');
        
    }
    
    public function usuarios($tipo = 'abertos'){
        
        $this->data['dados'] = array();
        
        if ($tipo == 'abertos'){
            $query = $this->db->query("SELECT c.usuario, u.nome, COUNT(c.id) as num "
                . "FROM chamado c INNER JOIN usuario u ON (c.usuario = u.id) "
                . "GROUP BY c.usuario");
        } else if ($tipo == 'andamento'){
            $query = $this->db->query("SELECT u.nome, COUNT(c.id) as num "
                    . "FROM fluxo f INNER JOIN usuario u ON (f.usuario_responsavel = u.id) "
                    . "INNER JOIN chamado c ON (f.chamado = c.id) "
                    . "WHERE c.status = 1 "
                    . "GROUP BY f.usuario_responsavel");
        } else if ($tipo == 'resolvidos'){
            $query = $this->db->query("SELECT u.nome, COUNT(c.id) as num "
                    . "FROM fluxo f INNER JOIN usuario u ON (f.usuario_responsavel = u.id) "
                    . "INNER JOIN chamado c ON (f.chamado = c.id) "
                    . "WHERE c.status = 3 "
                    . "GROUP BY f.usuario_responsavel");
        }
        
        if ($query->num_rows() > 0){
            $this->data['dados'] = $query->result();
        }
        
        $this->data['tipo'] = $tipo;
        
        $this->loadView('relatorio/usuarios');
        
    }
    
}

